package com.cspg.design.request;

import com.cspg.design.common.Constant;
import com.cspg.design.dto.Device;
import com.cspg.design.dto.PointDeviceDto;
import com.cspg.design.dto.PolylineDeviceDto;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.dto.submit.ProjectSubmitToReviewDto;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.http.RetrofitUtilReview;
import com.cspg.design.model.PointDevicesModel;
import com.cspg.design.model.PolylineDevicesModel;
import com.cspg.design.model.ProjectModel;
import com.cspg.design.model.ResponseResult;
import com.cspg.design.presenter.GTaskPresenter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GTask {
    private Call call;
    private final GTaskPresenter presenter;

    public GTask(GTaskPresenter presenter) {
        this.presenter = presenter;
    }

    public void updateProject(ProjectDto projectDto) throws Exception
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).updateProject(projectDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                ProjectDto projectModel= (ProjectDto) response.body();
                if (null == projectModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccess(projectDto);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void uploadAllPoint(List<PointDeviceDto> pointDeviceDtoList)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).addPointDeviceAll(pointDeviceDtoList);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                List<PointDeviceDto> pointDeviceDtoList = (List<PointDeviceDto>) response.body();
                if (null == pointDeviceDtoList) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccessPoints(pointDeviceDtoList,0);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }
    public void uploadAllPolyline(List<PolylineDeviceDto> polylineDeviceDtoList)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).addPolylineDeviceAll(polylineDeviceDtoList);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                List<PolylineDeviceDto> polylineDeviceDtoLis = (List<PolylineDeviceDto> ) response.body();
                if (null == polylineDeviceDtoLis) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccessPolylines(polylineDeviceDtoLis,0);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void uploadAllDevice(Device device)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).uploadAllDevice(device);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                }
                else if (response.code() == 500){
                    presenter.getFailure("项目数据出错！！！");
                }
                else if (response.code() == 404){
                    presenter.getFailure("设备和导线的数据对不上！！！");
                }
                else if (response.code() ==  400){
                    presenter.getFailure("设备数据保存失败！！！");
                }
                else if (response.code() ==  502)
                {
                    presenter.getFailure("导线数据保存失败！！！！");
                }
                else if (response.code() == 204)
                {
                    presenter.updateSuccess();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void getPointDeviceByProjectId(String projectId)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).getPointDeviceByProjectId(0,1000,projectId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                PointDevicesModel pointDevicesModel = (PointDevicesModel) response.body();
                if (null == pointDevicesModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccessPoints(pointDevicesModel.getContent(),1);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void getPolylineDeviceByProjectId(String projectId)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).getPolylineDeviceByProjectId(0,1000,projectId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                PolylineDevicesModel polylineDevicesModel = (PolylineDevicesModel) response.body();
                if (null == polylineDevicesModel) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                    return;
                }
                else
                {
                    presenter.requestSuccessPolylines(polylineDevicesModel.getContent(),1);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void deletePointDeviceByProjectId(String projectId)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).deletePointDeviceAll(projectId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                if (null == obj) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);

                }
                else
                {
                        presenter.deleteSuccessPoints(projectId);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

    public void deletePolylineDeviceByProjectId(String projectId)
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        call = retrofitUtil.createReq(IService.class).deletePolylineDeviceAll(projectId);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                if (null == obj) {
                    presenter.getFailure(Constant.HTTP_REQUEST_NET_WORK_ERROR);
                }
                else
                {
                    presenter.deleteSuccessPolylines(projectId);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }


    public void submitToReview(ProjectSubmitToReviewDto submitToReviewDto)
    {
        RetrofitUtilReview retrofitUtilReview = new RetrofitUtilReview();
        call = retrofitUtilReview.createReq(IService.class).projectSubmitToReview(submitToReviewDto);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                if (response.code() == 401)
                {
                    presenter.getTokenExpireFailure();
                    return;
                }

                if (response.code() == 204)
                {
                    presenter.updateSuccess();
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getFailure(Constant.HTTP_REQUEST_FAILURE);
                    return;
                }
                ResponseResult responseResult = (ResponseResult) response.body();
                if (null == responseResult || !responseResult.getResult().equals("200")) {
                    presenter.getFailure(Constant.HTTP_SERVER_RETURN_ERROR);
                }
                else
                {
                    presenter.submitToReviewSuccess();
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                presenter.getFailure(Constant.HTTP_NO_NET_WORK);
            }
        });
    }

}

